package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author cxl
 * @date 2020-02-29 21:51
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //根据用户名查询数据库获取用户信息和角色信息,同时还需要查询角色关联的权限信息
    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息,不包含用户的角色
        if (user==null){
            return null;
        }
        Integer userId = user.getId();//获取用户id
        //根据用户id查询用户角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色Id查询相关权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }


    //新增用户
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        Integer id = user.getId();//获取用户id  用于建立用户和角色之间多对多关系
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", id);
                map.put("roleId", roleId);
                userDao.setUserAndRole(map);
            }
        }
    }

    //根据id查询对应的用户   用于编辑用户信息回显
    public User findById(Integer id) {
        User byId = userDao.findById(id);
        String password = byId.getPassword();

        return byId;
    }

    //分页查询所有用户
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.findPage(queryString);
        long total = page.getTotal();
        List<User> result = page.getResult();
        return new PageResult(total, result);
    }

    //编辑用户信息
    public void edit(User user, Integer[] roleIds) {
        //基本信息修改
        userDao.edit(user);
        Integer id = user.getId();
        //删除第三张表中  对应的关系
        userDao.deleteUserIdAndRoleId(id);
        //重新设置关联关系
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", id);
                map.put("roleId", roleId);
                userDao.setUserAndRole(map);
            }
        }
    }

    //删除用户
    public void deleteById(Integer userId) {
        //删除第三张表中的对应关系
        userDao.deleteUserIdAndRoleId(userId);
        userDao.deleteUserById(userId);

    }
}
